$(function () {
    const MENU_TEMP = "<li><a href='javascript:void(0)'><i class='menu-icon'></i><span class='menu-text'></span><i class='menu-expand'></i></a><ul class='submenu'></ul></li>";

    function Menu(menu) {
        this.element = $(MENU_TEMP);
        this.$name = this.element.children("a").children("span.menu-text");
        this.$icon = this.element.children("a").children("i.menu-icon");
        this.$subMenu = this.element.children("ul.submenu");
        this.$expand = this.element.find("i.menu-expand");
        this.icon = menu.menuIcon;
        this.name = menu.menuName;
        this.link = menu.menuLink;
        this.children = [];
        var that = this;
        this.element.on("click", function () {
            if (that.children.length === 0) {
                if (activatedMenu) {
                    activatedMenu.unactive();
                }
                that.active();
                activatedMenu = that;
                // that.element.addClass("active").siblings().removeClass("active").removeClass("open");
            } else {
                if (that.element.hasClass("open")) {
                    that.close();
                } else {
                    that.open();
                }
            }
            return false;
        })

    }
    Menu.prototype.getElement = function () {
        this.$name.text(this.name);
        if (this.icon === undefined || this.icon === null || this.icon.length === 0) {
            this.$icon.remove();
        } else {
            this.$icon.addClass(this.icon);
        }

        return this.element;
    };
    Menu.prototype.addSubMenu = function (menu) {
        var child = new Menu(menu);
        child.removeExpand();
        this.$subMenu.append(child.getElement());
        this.children.push(child);
        return this;
    };
    Menu.prototype.removeExpand = function () {
        this.$expand.remove();
        return this;
    };
    Menu.prototype.open = function () {
        this.element.addClass("open").siblings().removeClass("open");
        return this;
    };
    Menu.prototype.close = function () {
        this.element.removeClass("open");
        return this;
    };
    Menu.prototype.active = function () {
        $("title").text(this.name);
        $("iframe").attr("src", this.link);
        this.element.addClass("active");
        return this;
    };
    Menu.prototype.unactive = function () {
        this.element.removeClass("active");
        return this;
    };
    // var homeMenu = new Menu({
    //     menuId: "000000",
    //     menuName: "首页",
    //     menuIcon: "glyphicon glyphicon-home",
    //     menuLink: "home.html"
    // }).removeExpand().active();
    // $("#menu").append(homeMenu.getElement());
    var activatedMenu = null;

    $.doAjax("auth/listTopMenu", function (topMenuList) {
        topMenuList.forEach(function (menu, topDx) {
            // console.log(menu);
            // console.log(isFirst);
            var topMenu = new Menu(menu);
            // console.log(topMenu);
            $.doAjax("auth/listSubMenu",{parentId: menu.menuId}, function (subMenuList) {
                if (subMenuList.length === 0) {
                    topMenu.removeExpand();
                } else {
                    subMenuList.forEach(function (menu) {
                        var menuObj = topMenu.addSubMenu(menu);
                    });
                }


                if (topDx === 0) {
                    activatedMenu = topMenu.children[0];
                    topMenu.open();
                    activatedMenu.active();
                }
            });
            $("#menu").append(topMenu.getElement());

        });
    });

    $("#refreshPower").on("click", function () {
        $.doAjax("auth/refreshPower", function (data) {
            console.log(data);
            $.confirm({
                title: "权限刷新成功",
                icon: "fa fa-question-circle-o",
                content: "<p>需要刷新页面吗？</p>",
                theme: "modern",
                type: "orange",
                buttons: {
                    ok: {
                        text: "刷新",
                        btnClass: "btn-success",
                        action: function () {
                           window.location.reload();
                        }
                    },
                    cancel: {
                        text: "取消",
                        btnClass: "btn-default",
                        action: function () {
                            // do nothing.
                        }
                    }
                }
            });
        });
    });
});